﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pavilion;

namespace Pavilion.Control
{
    public class Braket : Control
    {
        private CTexture2D texture;
        private int rangeAmount;

        public Braket(bool isLeft, int height) : base()
        {
            this.IsLeft = isLeft;

            AreaBounds = new Rectangle(0, 0, 5, height);
            OutlineColor = Color.Green;
            RangeAmount = 1;
            
            RenderTexture();
        }

        public void UpdatePosition(int index)
        {
            int positionX = index * 10;

            if (IsLeft)
            {
                int range = RangeAmount - 1;

                if (index < range)
                    range = index;

                positionX -= range * 10;
            }
            else
                positionX += RangeAmount * 10 - 4;

            this.Position = new Point(positionX, this.Position.Y);
        }

        private void RenderTexture()
        {
            Render render = new Render();

            render.Begin(AreaBounds.Width, AreaBounds.Height);

            DrawManager.Instance.FillRectangleOnScreen(new Rectangle(0, 0, 5, 5), Color.White);
            DrawManager.Instance.FillRectangleOnScreen(new Rectangle(0, AreaBounds.Height - 3, 5, 5), Color.White);

            if (IsLeft)
                DrawManager.Instance.FillRectangleOnScreen(new Rectangle(0, 0, 2, AreaBounds.Height), Color.White);
            else
                DrawManager.Instance.FillRectangleOnScreen(new Rectangle(3, 0, 2, AreaBounds.Height), Color.White);

            render.End();

            texture = new CTexture2D(render.Texture);
        }

        public override void Draw()
        {
            if (!Visible)
                return;

            texture.Draw(ScreenBounds, OutlineColor);

            base.Draw();
        }

        /// <summary>
        /// Gets or sets the position of this braket.
        /// </summary>
        public override Point Position
        {
            get { return base.Position; }
            set
            {
                if (base.Position.X == value.X)
                    return;

                base.Position = value;
            }
        }

        /// <summary>
        /// Gets or sets whether the braket is left or right sided.
        /// </summary>
        public bool IsLeft { get; set; }

        /// <summary>
        /// Gets or sets the amount of frames that will be visible from the index frame.
        /// </summary>
        public int RangeAmount
        {
            get { return rangeAmount; }
            set { if (value > 0) rangeAmount = value; }
        }
    }
}
